<?php get_main_menu("receptionist") ?>
   <br/>
   <div style="width:90%; margin-left:auto; margin-right:auto;">
    <div class="form_title">

            <?php echo $_REQUEST["action"];?> Order Item <?php echo $row_get_branch_by_id['name']?>
    </div>
    <div class="box"> 
<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "suborder")) {
  $insertSQL = sprintf("INSERT INTO suborder (order_id, branch_from_id, branch_to_id, branch_to_return_id, created_by, created_date, record_status, suborder_no) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['order_id'], "int"),
                       GetSQLValueString($_POST['branch_from_id'], "int"),
                       GetSQLValueString($_POST['branch_to_id'], "int"),
                       GetSQLValueString($_POST['branch_to_return_id'], "int"),
                       GetSQLValueString($loggedin_id, "int"),
                       GetSQLValueString(date("Y-m-d"), "date"),
                       GetSQLValueString($_POST['record_status'], "text"),
                       GetSQLValueString($_POST['suborder_no'], "text"));

  mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
  $Result1 = mysql_query($insertSQL, $drycleaners_conn) or die(mysql_error());
      if(mysql_affected_rows($drycleaners_conn)>0){
		  
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_get_suborder_last_insert = "SELECT * FROM drycleaners.suborder ORDER BY suborder.id DESC LIMIT 0,1";
$get_suborder_last_insert = mysql_query($query_get_suborder_last_insert, $drycleaners_conn) or die(mysql_error());
$row_get_suborder_last_insert = mysql_fetch_assoc($get_suborder_last_insert);
$totalRows_get_suborder_last_insert = mysql_num_rows($get_suborder_last_insert);



if (isset($_POST['suborder_items'])) {
			$optionArray = $_POST['suborder_items'];				
				
			for ($i=0; $i<count($optionArray); $i++) {
			$item_order_data=$optionArray[$i];
			
			echo "INSERT INTO suborder_item(suborder_id,item_order_id,created_by, created_date, record_status) 
			 VALUES('".$row_get_suborder_last_insert['id']."','$item_order_data','$loggedin_id','".date("Y-m-d")."','".$_POST['record_status']."')";
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$spd_insert=mysql_query("INSERT INTO suborder_item(suborder_id,order_item_id,created_by, created_date, record_status) 
VALUES('".$row_get_suborder_last_insert['id']."','$item_order_data','$loggedin_id','".date("Y-m-d")."','".$_POST['record_status']."')",$drycleaners_conn) or die(mysql_query());	 
			}
			redirect_drycleaners("suborder_view","2");
}
	  }
}

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_customer = "SELECT person.first_name, person.last_name, customer.id
  FROM    drycleaners.customer customer
       INNER JOIN
          drycleaners.person person
       ON (customer.person_id = person.id)";
$customer = mysql_query($query_customer, $drycleaners_conn) or die(mysql_error());
$row_customer = mysql_fetch_assoc($customer);
$totalRows_customer = mysql_num_rows($customer);

if(isset($_REQUEST['suborder_edit'])){
$colname_suborder = "-1";
if (isset($_GET['id'])) {
  $colname_suborder = $_GET['id'];
}
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_suborder = sprintf("SELECT * FROM drycleaners.suborder WHERE suborder.id = %s", GetSQLValueString($colname_suborder, "int"));
$suborder = mysql_query($query_suborder, $drycleaners_conn) or die(mysql_error());
$row_suborder = mysql_fetch_assoc($suborder);
$totalRows_suborder = mysql_num_rows($suborder);
}

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_suborder_last_insert = "SELECT * FROM drycleaners.suborder ORDER BY id DESC LIMIT 0 ,1";
$suborder_last_insert = mysql_query($query_suborder_last_insert, $drycleaners_conn) or die(mysql_error());
$row_suborder_last_insert = mysql_fetch_assoc($suborder_last_insert);
$totalRows_suborder_last_insert = mysql_num_rows($suborder_last_insert);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_branch = "SELECT * FROM branch";
$branch = mysql_query($query_branch, $drycleaners_conn) or die(mysql_error());
$row_branch = mysql_fetch_assoc($branch);
$totalRows_branch = mysql_num_rows($branch);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_branch_to = "SELECT * FROM branch";
$branch_to = mysql_query($query_branch_to, $drycleaners_conn) or die(mysql_error());
$row_branch_to = mysql_fetch_assoc($branch_to);
$totalRows_branch_to = mysql_num_rows($branch_to);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_get_transfered = "SELECT suborder_item.order_item_id
  FROM    (   drycleaners.suborder suborder
           INNER JOIN
              drycleaners.`order` `order`
           ON (suborder.order_id = `order`.id))
       INNER JOIN
          drycleaners.suborder_item suborder_item
       ON (suborder_item.suborder_id = suborder.id)
 WHERE `order`.id ='".$_REQUEST['order_id']."' AND suborder.record_status='active'";
$get_transfered = mysql_query($query_get_transfered, $drycleaners_conn) or die(mysql_error());
$row_get_transfered = mysql_fetch_assoc($get_transfered);
$totalRows_get_transfered = mysql_num_rows($get_transfered);

$transfered="";
$i=0;
if($totalRows_get_transfered>0){
	do{
		$transfered[$i]=$row_get_transfered["order_item_id"];
		$i++;
		}while ($row_get_transfered = mysql_fetch_assoc($get_transfered));
		$transfered="'".implode("','",$transfered)."'";
		$transfered="AND order_item.id NOT IN ($transfered)";
		
		echo $transfered;
	}

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_items = "SELECT material.name as material,        type_of_service.name as type_of_service,        order_item.color,        order_item.tag,        order_item.id,        item.name as item   FROM    (   (   drycleaners.order_item order_item                INNER JOIN                   drycleaners.item item                ON (order_item.item_id = item.id))            INNER JOIN               drycleaners.material material            ON (order_item.material_id = material.id))        INNER JOIN           drycleaners.type_of_service type_of_service        ON (order_item.type_of_service_id = type_of_service.id) where order_id='".$_REQUEST['order_id']."' $transfered";
$order_items = mysql_query($query_order_items, $drycleaners_conn) or die(mysql_error());
$row_order_items = mysql_fetch_assoc($order_items);
$totalRows_order_items = mysql_num_rows($order_items);
?>

<form action="<?php echo $editFormAction; ?>" method="POST" name="suborder" id="suborder">
  <table align="left">
   <tr valign="baseline">
      <td nowrap="nowrap" align="left">Suborder No:</td>
      <td><input type="hidden" name="id" value="<?php echo $row_suborder['id']; ?>" size="32" />
      <input type="hidden" name="suborder_no" value="<?php if(isset($_REQUEST['suborder_edit'])){echo $row_suborder['suborder_no'];} else{ echo "SOT".(1+$row_suborder_last_insert['id']);} ?>" size="32" /><?php if(isset($_REQUEST['suborder_edit'])){echo $row_suborder['suborder_no'];} else{ echo "SOT".(1+$row_suborder_last_insert['id']);} ?>
      </td>
    </tr>
    
        <tr valign="baseline">
      <td nowrap="nowrap" align="left">Branch To:</td>
      <td><select name="branch_to_id">
        <?php 
do {  
?>
        <option value="<?php echo $row_branch['id']?>" ><?php echo $row_branch['name']?></option>
        <?php
} while ($row_branch = mysql_fetch_assoc($branch));
?>
      </select></td>
    </tr>
        <tr valign="baseline">
      <td nowrap="nowrap" align="left">Return Branch:</td>
      <td><select name="branch_to_return_id">
        <?php 
do {  
?>
        <option value="<?php echo $row_branch_to['id']?>" ><?php echo $row_branch_to['name']?></option>
        <?php
} while ($row_branch_to = mysql_fetch_assoc($branch_to));
?>
      </select></td>
    </tr>
    <?php if($totalRows_order_items >0){?>
    <tr> <td><strong>What To Transfer</strong></td>
    <td>
    	<?php do { ?>
 <input type='checkbox' name='suborder_items[]' value='<?php echo $row_order_items['id']; ?>' /><?php echo $row_order_items['material']." ".$row_order_items['color']." ".$row_order_items['item'].",<strong>Service Type:</strong> ".$row_order_items['type_of_service']." <strong>Tag</strong>:".$row_order_items['tag']."<br/>"; ?>
 <?php } while ($row_order_items = mysql_fetch_assoc($order_items)); ?>
    </td></tr><?php } else{echo "No Items To Transfer";}?>
    <tr valign="baseline">
    <td></td>
      <td nowrap="nowrap" align="left"> 
      <?php if(isset($_REQUEST['suborder_edit'])){	
		echo"<input name='update' type='submit' value='UPDATE' id='submit'/>";
		echo" <a href='.?suborder_view&order_id=".$_REQUEST['order_id']."&back_page=order_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
		echo"<input type='hidden' name='MM_update' value='suborder_form' />";	
		}
		else{
			echo"<input name='save' type='submit' value='Continue' id='submit'/>";
			echo" <a href='.?suborder_view&order_id=".$_REQUEST['order_id']."&back_page=order_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
			echo"<input type='hidden' name='MM_insert' value='suborder_form' />";	
			}
		?>
      </td>
      <td>	  </td>
    </tr>
  </table>
  <input type="hidden" name="record_status" value="active" />
  <input type="hidden" name="branch_from_id" value="<?php echo $_SESSION['branch_id']?>" />
   <input type="hidden" name="order_id" value="<?php echo $_REQUEST['order_id']?>" />
    <input type="hidden" name="id" value="<?php echo $row_suborder['id']?>" />
  <input type="hidden" name="MM_insert" value="suborder" />
</form>
<?php
mysql_free_result($branch);
mysql_free_result($order_items);

mysql_free_result($get_transfered);
mysql_free_result($customer);
?>
</div>